local addonName,ns = ...
local L = ns.L
ns.CVarFrames = {TextFrames = {},Frames = {}}--记录CVar框体
ns.Y = {}	--用于分页行数自动拓展

--创建主页面点击放大分类按钮
local tabbutton = {}
function ns.AddClickBC(parent,frame, id, width, text, xOffset, yOffset)
	local xOffset = xOffset or 6
    local yOffset = yOffset or -2
    local prevButton = tabbutton[id - 1] -- 获取前一个按钮

    local button = CreateFrame("Button", nil, parent, "ColumnDisplayButtonTemplate")
    button:SetSize(width, 24) -- 如果不是24，右边框体会出问题
    if prevButton then
		frame:Hide()
        button:SetPoint("BOTTOMLEFT", prevButton["A"], "BOTTOMRIGHT", 10 ,0)
		button:SetScale(.9)
		button:GetFontString():SetTextColor(1, 1, 1)
    else
        button:SetPoint("BOTTOM", parent, "TOPLEFT", xOffset, yOffset)
		button:SetScale(1.1)
		button:GetFontString():SetTextColor(1, 1, 0)
    end
    button:SetText(text)
    button:GetFontString():SetPoint("TOPLEFT", button, "TOPLEFT", 0, 0)
    button:GetFontString():SetPoint("BOTTOMRIGHT", button, "BOTTOMRIGHT", 0, 0)
	
    tabbutton[id] = {}
	tabbutton[id]["A"] = button
	tabbutton[id]["B"] = frame
    button:SetScript("OnClick", function(self)
        for aa, row in pairs(tabbutton) do
            if self == row["A"] then -- 只对当前按钮重新定位
                row["A"]:SetScale(1.1)
				row["A"]:GetFontString():SetTextColor(1, 1, 0)
				row["B"]:Show()
            else
                row["A"]:SetScale(.9)
				row["A"]:GetFontString():SetTextColor(1, 1, 1)
				row["B"]:Hide()
            end
        end
    end)

    return button
end

--点击功能按钮
function ns.AddfuncButton(parent,point,name,x,y,width,height)
	local width = width or 140
	local height = height or 25
	local x = x or 470
	local y = y or 1
	local funcButton = CreateFrame("Button", nil, parent, "UIPanelButtonTemplate")
	funcButton:SetText(name)
	funcButton:SetSize(width,height)
	funcButton:SetPoint("LEFT",point,"LEFT", x, y)
	funcButton:SetScript("OnClick", function()end)
	return funcButton
end

--色块
function ns.AddColorFrame(parent, x, y, tip,width,height,DB,DB2,DB3)
	local parent = parent or UIParent	--父框体
	local x, y = x or 0, y or 0	--锚点坐标
	local r, g, b = 0, 1, 0
	local tip = tip or L["点击更改颜色"]
	local width,height = width or 75,height or 15
	if DB3 then
		r = PlateColorDB[DB][DB2][DB3]["r"]
		g = PlateColorDB[DB][DB2][DB3]["g"]
		b = PlateColorDB[DB][DB2][DB3]["b"]
	elseif DB2 then
		r = PlateColorDB[DB][DB2]["r"]
		g = PlateColorDB[DB][DB2]["g"]
		b = PlateColorDB[DB][DB2]["b"]
	elseif DB then
		r = PlateColorDB[DB]["r"]
		g = PlateColorDB[DB]["g"]
		b = PlateColorDB[DB]["b"]
	end

    parent.btn = CreateFrame("Button", nil, parent, "GameMenuButtonTemplate");
    parent.btn:SetPoint("TOPLEFT", parent, "TOPLEFT", x, y);
    parent.btn:SetSize(width,height);
    parent.btn:SetAlpha(0)
    parent.btn:SetNormalFontObject("GameFontNormalLarge");
    parent.btn:SetHighlightFontObject("GameFontHighlightLarge");
	parent.btn.color = parent:CreateTexture(nil, "ARTWORK")
    parent.btn.color:SetPoint("TOPLEFT", parent, "TOPLEFT", x, y)
    parent.btn.color:SetSize(width,height)
    parent.btn.color:SetColorTexture(r, g, b)
	
    local onUpdate = function(restore)
        local r, g, b = ColorPickerFrame:GetColorRGB()
        parent.btn.color:SetColorTexture(r, g, b)
		if DB3 then
			PlateColorDB[DB][DB2][DB3]["r"] = r
			PlateColorDB[DB][DB2][DB3]["g"] = g
			PlateColorDB[DB][DB2][DB3]["b"] = b
			PlateColorDB[DB][DB2]["set"] = true;
		elseif DB2 then
			PlateColorDB[DB][DB2]["r"] = r
			PlateColorDB[DB][DB2]["g"] = g
			PlateColorDB[DB][DB2]["b"] = b
		elseif DB then
			PlateColorDB[DB]["r"] = r
			PlateColorDB[DB]["g"] = g
			PlateColorDB[DB]["b"] = b
		end
    end
    local onCancel = function()
        local r, g, b = ColorPickerFrame:GetPreviousValues()
        parent.btn.color:SetColorTexture(r, g, b)
		if DB3 then
			PlateColorDB[DB][DB2][DB3]["r"] = r
			PlateColorDB[DB][DB2][DB3]["g"] = g
			PlateColorDB[DB][DB2][DB3]["b"] = b
		elseif DB2 then
			PlateColorDB[DB][DB2]["r"] = r
			PlateColorDB[DB][DB2]["g"] = g
			PlateColorDB[DB][DB2]["b"] = b
		elseif DB then
			PlateColorDB[DB]["r"] = r
			PlateColorDB[DB]["g"] = g
			PlateColorDB[DB]["b"] = b
		end
    end

    parent.btn:SetScript("OnClick", function(self, button, down)
       local r,g,b = r, g, b
       ColorPickerFrame.swatchFunc = onUpdate
       ColorPickerFrame.previousValues = {r = r, g = g, b = b}
       ColorPickerFrame.cancelFunc = onCancel
       ColorPickerFrame.Content.ColorPicker:SetColorRGB(r, g, b)
       ColorPickerFrame:Show()
    end)
    parent.btn:SetScript("OnEnter", function(self)
        GameTooltip:SetOwner(self, "ANCHOR_CURSOR ", 20, -20)
        GameTooltip:SetText(tip)
        GameTooltip:Show()
    end)
    parent.btn:SetScript("OnLeave", function(self)
        GameTooltip:Hide()
    end)
	return parent.btn
end

--创建主页面编辑框
function ns.AddEditBox(parent,x,y,size,Numeric,scale)
	local Box = CreateFrame("EditBox", nil, parent, "InputBoxTemplate")
	Box:SetSize(size, 30)
	Box:SetScale(scale or 1)
	Box:SetPoint("TOPLEFT", x,y)
	Box:SetAutoFocus(false)
	Box:SetNumeric(Numeric)
	local ClearBox = CreateFrame("Button", nil, Box)
	ClearBox:SetPoint("RIGHT", -5, 0)
	ClearBox:SetSize(13, 13)
	ClearBox:SetNormalTexture("common-search-clearbutton")
	ClearBox:SetHighlightTexture("common-search-clearbutton")
	Box:SetScript("OnEnterPressed", function() end)
	--Box:SetScript("OnTextChanged", function() end)
	Box:SetScript("OnTextChanged", function(self)
		ClearBox:SetShown(self:GetText() ~= "")
	end)
	ClearBox:SetScript("OnClick", function() Box:SetText("") Box:GetScript("OnTextChanged")(Box)  end)
	return Box
end

--创建主页面点击按钮
function ns.AddButtons(parent,name,x,y,width,height,Numeric,scale)
	local Button = CreateFrame("Button", nil, parent, "UIPanelButtonTemplate")
	Button:SetPoint("TOPLEFT", x,y)
	Button:SetSize(width,height)
	Button:SetText(name)
	Button:SetScript("OnClick", function() end)
	return Button
end

--设置主页面分类标题文本
function ns.AddSetTiText(parent,y,texts)
	local Text = parent:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
	Text:SetPoint("TOPLEFT",parent,"TOPLEFT", 30, -20+ns.Y[y]*-35)
	Text:SetText(texts)
	Text:SetFont(ns.fonts, 20, "OUTLINE")
	Text:SetTextColor(1,1,1)
	ns.Y[y] = ns.Y[y] + 1	--最后增加一次起始位置
	return Text
end
--设置主页面点击按钮
function ns.AddSetClickB(parent,y,name,tip,db,cvar,spare1,spare2)
	local rowFrame = CreateFrame("Frame", nil, parent)
    rowFrame:SetSize(630, 26)
    rowFrame:SetPoint("TOPLEFT", 10, -10+ns.Y[y]*-35)
	local SliderBackground = rowFrame:CreateTexture(nil, "BACKGROUND")
	SliderBackground:SetTexture(130937)
	SliderBackground:SetAllPoints(rowFrame)
	SliderBackground:SetColorTexture(0, 0, 0, 0) -- 设置背景颜色为黑色，透明度为0.5
	SliderBackground:SetScript("OnEnter", function(self)
		self:SetColorTexture(0.5, 0.5, 0.5, .2)
	end)
	SliderBackground:SetScript("OnLeave", function(self)
		self:SetColorTexture(0, 0, 0, 0)
	end)
	local check = CreateFrame("CheckButton", nil, rowFrame, "InterfaceOptionsCheckButtonTemplate")
	check:SetPoint("LEFT",SliderBackground,"LEFT", 297, 0)
	check:SetSize(30,30)
	check:SetChecked(PlateColorDB[db])
	check:SetScript("OnEnter",function(self) 
		SliderBackground:SetColorTexture(0.5, 0.5, 0.5, .2)
		if tip then
			GameTooltip:SetOwner(SliderBackground, "ANCHOR_TOPRIGHT")
			GameTooltip:AddLine("|cffFFFFFF"..tip.."|r") 
			GameTooltip:Show()
		end
	end)
	check:SetScript("OnLeave", function(self)    
		SliderBackground:SetColorTexture(0, 0, 0, 0)
		if tip then
			GameTooltip:Hide()
		end
	end)
	
	check:SetScript("OnClick", function ( ... )
		PlateColorDB[db] = check:GetChecked()
		if InCombatLockdown() then return end
		if cvar and cvar == "nameplateShowOnlyNames" then
			ns.CteatNewPlateName()
			if check:GetChecked() then
				SetCVar(cvar, 1)
				C_Timer.After(0.1,function()--延迟设置,不然会被重置
					C_NamePlate.SetNamePlateFriendlySize(PlateColorDB.friendnpX,PlateColorDB.friendnpY)
				end)
			else
				SetCVar(cvar, 0)
			end
		elseif cvar then
			if check:GetChecked() then
				SetCVar(cvar, 1)
			else
				SetCVar(cvar, 0)
			end
		end
	end)
	local lefttext = rowFrame:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
	lefttext:SetPoint("LEFT", SliderBackground, "LEFT", 90, 0)
	lefttext:SetText(name)
	lefttext:SetFont(ns.fonts, 14, "OUTLINE")
	if cvar and cvar ~= "OUTLINE" then--如果是CVar设置字体颜色为绿色
		lefttext:SetTextColor(0,1,0)
		table.insert(ns.CVarFrames["TextFrames"], lefttext)
		table.insert(ns.CVarFrames["Frames"], check)
	else
		lefttext:SetTextColor(1,.82,0)
	end
		
	
	ns.Y[y] = ns.Y[y] + 1	--最后增加一次起始位置
	local cleckandtext = {}
	cleckandtext.text = lefttext
	cleckandtext.check = check
	return cleckandtext
	
end

--设置主页面滑动条
local OptionFrames = {}
local OptionFrameid = 1
function ns.AddSetSlider(parent,y, name, tip, minValue, maxValue, valueStep,varformat, db,cvar,spare)
	local rowFrame = CreateFrame("Frame", nil, parent)
    rowFrame:SetSize(630, 26)
    rowFrame:SetPoint("TOPLEFT", 10, -10+ns.Y[y]*-35)
	local SliderBackground = rowFrame:CreateTexture(nil, "BACKGROUND")
	SliderBackground:SetTexture(130937)
	SliderBackground:SetColorTexture(0, 0, 0, 0) -- 设置背景颜色为黑色，透明度为0.5
	SliderBackground:SetAllPoints(rowFrame)
	
	local PCSlider = CreateFrame("Slider", nil, rowFrame, "OptionsSliderTemplate")
	PCSlider:SetPoint("LEFT",SliderBackground,"LEFT", 300, 0)
	PCSlider:SetSize(220,20)
	PCSlider:SetMinMaxValues(minValue, maxValue)
	PCSlider:SetValue(PlateColorDB[db])
	PCSlider:SetValueStep(valueStep)
	PCSlider:SetObeyStepOnDrag(true)
	PCSlider.lefttext = PCSlider:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
	PCSlider.lefttext:SetPoint("LEFT", SliderBackground, "LEFT", 90, 0)
	PCSlider.lefttext:SetText(name)
	PCSlider.lefttext:SetFont(ns.fonts, 14, "OUTLINE")
	PCSlider.righttext = PCSlider:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
	PCSlider.righttext:SetPoint("LEFT",PCSlider,"RIGHT",10, 0)
	PCSlider.righttext:SetFont(ns.fonts, 17, "OUTLINE")
	PCSlider.righttext:SetTextColor(0,1,0)
	PCSlider.righttext:SetText(tonumber(string.format(varformat,PlateColorDB[db])))
	if cvar and cvar ~=1 then--如果是CVar设置字体颜色为绿色
		PCSlider.lefttext:SetTextColor(0,1,0)
		table.insert(ns.CVarFrames["TextFrames"], PCSlider.lefttext)
		table.insert(ns.CVarFrames["TextFrames"], PCSlider.righttext)
		table.insert(ns.CVarFrames["Frames"], PCSlider)
	else
		PCSlider.lefttext:SetTextColor(1,.82,0)
	end
	PCSlider.Low:Hide()
	PCSlider.High:Hide()
	PCSlider:SetScript("OnValueChanged", function(self, value)
		self.righttext:SetText(string.format(varformat,value))
		PlateColorDB[db] = tonumber(string.format(varformat,value))
		if InCombatLockdown() then return end
		if cvar and cvar == "nameplateMaxAlpha" then--如果是非当前目标透明度
			SetCVar(cvar, value)
			SetCVar(spare, value)
		elseif cvar and cvar == 1 then--如果是1则处理友方名字
			ns.CteatNewPlateName()
		elseif cvar and cvar == 2 then--如果是2则是友方血条尺寸,备注是另一项
			C_NamePlate.SetNamePlateFriendlySize(PlateColorDB[db],PlateColorDB[spare])
		elseif cvar and cvar == 3 then--如果是3则是友方姓名版上下,备注是另一项
			C_NamePlate.SetNamePlateFriendlySize(PlateColorDB[spare],PlateColorDB[db])
		elseif cvar then
			SetCVar(cvar, value)	--如果是cvar执行一次
		end

	end)
	SliderBackground:SetScript("OnEnter", function(self)
		self:SetColorTexture(0.5, 0.5, 0.5, .2)
	end)
	SliderBackground:SetScript("OnLeave", function(self)
		self:SetColorTexture(0, 0, 0, 0)
	end)
	PCSlider:SetScript("OnEnter",function(self)
		SliderBackground:SetColorTexture(0.5, 0.5, 0.5, .2)
		if tip then
			GameTooltip:SetOwner(SliderBackground, "ANCHOR_TOPRIGHT") 
			GameTooltip:AddLine("|cffFFFFFF"..tip.."|r") 
			GameTooltip:Show()
		end
	end)
	PCSlider:SetScript("OnLeave", function(self)
		SliderBackground:SetColorTexture(0, 0, 0, 0)
		if tip then
			GameTooltip:Hide()
		end
	end)
	ns.Y[y] = ns.Y[y] + 1	--最后增加一次起始位置
	local PCSliders = {}
	PCSliders.check = PCSlider
	PCSliders.text = PCSlider.lefttext
	PCSliders.righttext = PCSlider.righttext
	return PCSliders
end

--设置主页面材质下拉菜单
function ns.AddSetDropdTexture(parent,y,name,tip,db,TextureTable)
	local rowFrame = CreateFrame("Frame", nil, parent)
    rowFrame:SetSize(630, 26)
    rowFrame:SetPoint("TOPLEFT", 10, -10+ns.Y[y]*-35)
	local SliderBackground = rowFrame:CreateTexture(nil, "BACKGROUND")
	SliderBackground:SetTexture(130937)
	SliderBackground:SetColorTexture(0, 0, 0, 0) -- 设置背景颜色为黑色，透明度为0.5
	SliderBackground:SetAllPoints(rowFrame)
	
	local lefttext = rowFrame:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
	lefttext:SetPoint("LEFT", SliderBackground, "LEFT", 90, 0)
	lefttext:SetText(name)
	lefttext:SetFont(ns.fonts, 14, "OUTLINE")
	lefttext:SetTextColor(1,.82,0)
	
	local cpHpstatusbar = rowFrame:CreateTexture(nil, "BACKGROUND")
	cpHpstatusbar:SetPoint("RIGHT",rowFrame,"RIGHT", -5, 0)
	cpHpstatusbar:SetSize(152,13)
	cpHpstatusbar:SetVertexColor(1,0,0,1)

	local cpHpstatusbarBG = CreateFrame("Frame", nil, rowFrame, "BackdropTemplate")
	cpHpstatusbarBG:SetPoint("TOPLEFT",cpHpstatusbar,"TOPLEFT",-1,1.5)
	cpHpstatusbarBG:SetPoint("BOTTOMRIGHT",cpHpstatusbar,"BOTTOMRIGHT",1.5,-1)
	cpHpstatusbarBG:SetBackdrop{edgeFile = 'Interface\\Buttons\\WHITE8x8',edgeSize = 2}
	cpHpstatusbarBG:SetBackdropBorderColor(.5, .5, .5, 1)
	if TextureTable[PlateColorDB.hpTexture] then
		cpHpstatusbar:SetTexture(TextureTable[PlateColorDB.hpTexture])
	else
		cpHpstatusbar:SetTexture(TextureTable["PlateColor"])
	end
	SliderBackground:SetScript("OnEnter", function(self)
		self:SetColorTexture(0.5, 0.5, 0.5, .2)
		cpHpstatusbarBG:SetBackdropBorderColor(1, 1, 1, 1)
	end)
	SliderBackground:SetScript("OnLeave", function(self)
		self:SetColorTexture(0, 0, 0, 0)
		cpHpstatusbarBG:SetBackdropBorderColor(.5, .5, .5, 1)
	end)
	
	--下拉菜单
    local RadioDropdown = CreateFrame("DropdownButton", nil, rowFrame, "WowStyle1DropdownTemplate")
    RadioDropdown:SetPoint("LEFT",SliderBackground, 300, 0)
	RadioDropdown:SetDefaultText(PlateColorDB[db])
    RadioDropdown:SetWidth(170)
	
    local function IsSelected(value)
        return value == PlateColorDB[db]
    end
    local function SetSelected(value)
        PlateColorDB[db] = value
		cpHpstatusbar:SetTexture(TextureTable[PlateColorDB[db]])
    end
	
    -- 菜单项生成函数
    local function GeneratorFunction(dropdown, rootDescription)
		rootDescription:SetScrollMode(400);
        for _, text in pairs(ns.UseHpTextures) do
			local texts = text
			if string.match(TextureTable[text],"PlateColor\\texture")then 
				texts = "|cff00FFFF"..text
			end
			if text == "noHpTexture" then
				texts = "|cff00FFFF"..L["不修改材质"]
			end
            local radio = rootDescription:CreateRadio(texts, IsSelected, SetSelected, text)
            radio:AddInitializer(function(button, description, menu)
                local bgTexture = button:AttachTexture()
				bgTexture:SetSize(200,18)
                bgTexture:SetPoint("LEFT",15,0);
                bgTexture:SetTexture(TextureTable[text])
            end)
        end
    end
    RadioDropdown:SetupMenu(GeneratorFunction)
	
	ns.Y[y] = ns.Y[y] + 1	--最后增加一次起始位置
	return check
end

--设置主页面下拉菜单
function ns.AddSetDropdM(parent,y,name,tip,optionstext,db)
	local rowFrame = CreateFrame("Frame", nil, parent)
    rowFrame:SetSize(630, 26)
    rowFrame:SetPoint("TOPLEFT", 10, -10+ns.Y[y]*-35)
	local SliderBackground = rowFrame:CreateTexture(nil, "BACKGROUND")
	SliderBackground:SetTexture(130937)
	SliderBackground:SetColorTexture(0, 0, 0, 0) -- 设置背景颜色为黑色，透明度为0.5
	SliderBackground:SetAllPoints(rowFrame)
	SliderBackground:SetScript("OnEnter", function(self)
		self:SetColorTexture(0.5, 0.5, 0.5, .2)
	end)
	SliderBackground:SetScript("OnLeave", function(self)
		self:SetColorTexture(0, 0, 0, 0)
	end)
	
	local lefttext = rowFrame:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
	lefttext:SetPoint("LEFT", SliderBackground, "LEFT", 90, 0)
	lefttext:SetText(name)
	lefttext:SetFont(ns.fonts, 14, "OUTLINE")
	lefttext:SetTextColor(1,.82,0)
	
	--下拉菜单
	local RadioDropdown = CreateFrame("DropdownButton", nil, rowFrame, "WowStyle1DropdownTemplate")
	RadioDropdown:SetPoint("LEFT",SliderBackground,300, 0)
	local function IsSelected(value)
		return value == PlateColorDB[db]
	end

	local function SetSelected(value)
		PlateColorDB[db] = value
	end
	MenuUtil.CreateRadioMenu(RadioDropdown,IsSelected, SetSelected,unpack(optionstext))
		
	ns.Y[y] = ns.Y[y] + 1	--最后增加一次起始位置
	return check
end

--设置主页面界面颜色选项
function ns.AddSetColorF(parent,y,name,tip,DB,DB2,DB3)
	local rowFrame = CreateFrame("Frame", nil, parent)
    rowFrame:SetSize(630, 26)
    rowFrame:SetPoint("TOPLEFT", 10, -10+ns.Y[y]*-35)
	local SliderBackground = rowFrame:CreateTexture(nil, "BACKGROUND")
	SliderBackground:SetTexture(130937)
	SliderBackground:SetAllPoints(rowFrame)
	SliderBackground:SetColorTexture(0, 0, 0, 0) -- 设置背景颜色为黑色，透明度为0.5
	SliderBackground:SetAllPoints(rowFrame)
	SliderBackground:SetScript("OnEnter", function(self)
		self:SetColorTexture(0.5, 0.5, 0.5, .2)
	end)
	SliderBackground:SetScript("OnLeave", function(self)
		self:SetColorTexture(0, 0, 0, 0)
	end)
	
	local lefttext = rowFrame:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
	lefttext:SetPoint("LEFT", SliderBackground, "LEFT", 90, 0)
	lefttext:SetText(name)
	lefttext:SetFont(ns.fonts, 14, "OUTLINE")
	lefttext:SetTextColor(1,.82,0)
	
	local Color = ns.AddColorFrame(rowFrame, 301, -3, "",150,20,DB,DB2,DB3)
	Color:HookScript("OnEnter", function(self)
		SliderBackground:SetColorTexture(0.5, 0.5, 0.5, .2)
		if tip then
			GameTooltip:SetOwner(SliderBackground, "ANCHOR_TOPRIGHT") 
			GameTooltip:AddLine("|cffFFFFFF"..tip.."|r") 
			GameTooltip:Show()
		end
	end)
	Color:HookScript("OnLeave", function(self)
		SliderBackground:SetColorTexture(0, 0, 0, 0)
		if tip then
			GameTooltip:Hide()
		end
	end)
	
	ns.Y[y] = ns.Y[y] + 1	--最后增加一次起始位置
	
	return Color
end

--设置主页面创建表格分类按钮分类
function ns.AddClassB(parent,text,width,x,y,func)
	local y = y or -2
	local button1 = CreateFrame("Button", nil, parent, "ColumnDisplayButtonTemplate")
	button1:SetSize(width,24)--不是24右边框体会出问题
	button1:SetPoint("BOTTOMLEFT", parent, "TOPLEFT",x,y)
	button1:SetText(text)
	button1:GetFontString():SetPoint("TOPLEFT", button1, "TOPLEFT", 0, 0)
	button1:GetFontString():SetPoint("BOTTOMRIGHT", button1, "BOTTOMRIGHT", 0, 0)
	button1:SetScript("OnClick", func or (function() end))
	return button1
end

-- 创建多选下拉菜单的函数
local function CreateDropdownMenu(parent,x,y,db)
    local dropdown = CreateFrame("Frame", nil, parent, "UIDropDownMenuTemplate")
    dropdown:SetPoint("LEFT",x,y) -- 设置下拉菜单的位置
	UIDropDownMenu_SetText(dropdown, "选项")
    
    -- 设置下拉菜单的初始化函数
    UIDropDownMenu_Initialize(dropdown, function(self, level, menuList)
        local info = UIDropDownMenu_CreateInfo()

        -- 遍历选项并添加到下拉菜单中
        for _, optionData in pairs(db) do
            info.text = optionData.text
            info.checked = optionData.checked
            info.func = function() 
                -- 更新dropDB的值
                optionData.checked = not optionData.checked
                ToggleDropDownMenu(1, nil, dropdown)
            end
            UIDropDownMenu_AddButton(info, level)
        end
    end)
end

--表格内勾选按钮和文字
function ns.AddTableClick(parent,y,name,DB,DB2,DB3)
	local click = false
	if DB3 then
		click = PlateColorDB[DB][DB2][DB3]
	elseif DB2 then
		click = PlateColorDB[DB][DB2]
	elseif DB then
		click = PlateColorDB[DB]
	end
	local background = parent:CreateTexture(nil, "ARTWORK")
	background:SetTexture(130937)
	background:SetPoint("LEFT",parent,"LEFT", 232+y, 0)
	background:SetSize(65,23)
	background:SetColorTexture(0, 0, 0, .5) -- 设置背景颜色为黑色，透明度为0.5
	local check = CreateFrame("CheckButton", nil, parent, "InterfaceOptionsCheckButtonTemplate")
	check:SetPoint("LEFT",background,"LEFT", 0, 0)
	check:SetChecked(click)
	check:SetScript("OnClick", function (self)
		if DB3 then
			PlateColorDB[DB][DB2][DB3] = self:GetChecked()
			PlateColorDB[DB][DB2]["set"] = true;
		elseif DB2 then
			PlateColorDB[DB][DB2] = self:GetChecked()
			PlateColorDB[DB]["set"] = true;
		elseif DB then
			PlateColorDB[DB] = self:GetChecked()
		end
	end)
	local text = parent:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
	text:SetPoint("LEFT", check, "RIGHT", 0, 1)
	text:SetText(name)
	text:SetFont(ns.fonts, 14, "OUTLINE")
	text:SetTextColor(1,1,1,.83)
	local cleckandtext = {}
	cleckandtext.text = text
	cleckandtext.check = check
	return cleckandtext
end

--表格行输入框
function ns.AddTableNotes(parent,y,DB,DB2,DB3)
	local text = 1
	if DB3 then
		text = PlateColorDB[DB][DB2][DB3]
	elseif DB2 then
		text = PlateColorDB[DB][DB2]
	elseif DB then
		text = PlateColorDB[DB]
	end
	if not text then text = 1 end
	if DB3 then
		PlateColorDB[DB][DB2][DB3] = tonumber(text)
	elseif DB2 then
		PlateColorDB[DB][DB2] = tonumber(text)
	elseif DB then
		PlateColorDB[DB] = tonumber(text)
	end
	local background = parent:CreateTexture(nil, "ARTWORK")
	background:SetTexture(130937)
	background:SetPoint("LEFT",parent,"LEFT", 232+y, 0)
	background:SetSize(65,23)
	background:SetColorTexture(0, 0, 0, .5) -- 设置背景颜色为黑色，透明度为0.5
	local InputBox = CreateFrame("EditBox", nil, parent, "InputBoxTemplate")
	InputBox:SetSize(25, 20)
	InputBox:SetPoint("LEFT", 245+y,0)
	InputBox:SetAutoFocus(false) -- 不自动获取焦点
	InputBox:SetJustifyH("CENTER")
	InputBox:SetMultiLine(true)
	InputBox:SetMaxLetters(2)
	InputBox:SetText(text)
	InputBox:SetTextInsets(-5,0,0,0) --文字可用位置
	InputBox:SetTextColor(1,1,1,.83)--(0, 1, 1, 1)
	InputBox:SetScript("OnEnterPressed", function(self)
		self:ClearFocus() -- 清除编辑框的焦点
	end)
	InputBox:SetScript("OnEditFocusLost", function(self)
		if self:GetText() == "" then
			InputBox:SetText(1)
		end
		if DB3 then
			PlateColorDB[DB][DB2][DB3] = tonumber(self:GetText())
			PlateColorDB[DB][DB2]["set"] = true;
		elseif DB2 then
			PlateColorDB[DB][DB2] = tonumber(self:GetText())
			PlateColorDB[DB]["set"] = true;
		elseif DB then
			PlateColorDB[DB] = tonumber(self:GetText())
		end
	end)
	local defaultText = InputBox:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
	defaultText:SetPoint("LEFT",InputBox,"RIGHT", 3, 0)
	defaultText:SetText(L["层数"])
	defaultText:SetTextColor(1,1,1,.83)
	
	return InputBox
end